package com.limingru.online_class.service.impl;

import com.limingru.online_class.config.CacheKeyManager;
import com.limingru.online_class.domain.Video;
import com.limingru.online_class.domain.VideoBanner;
import com.limingru.online_class.domain.vo.VideoVO;
import com.limingru.online_class.mapper.VideoMapper;
import com.limingru.online_class.service.VideoService;
import com.limingru.online_class.utils.CacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    VideoMapper videoMapper;

    @Autowired
    CacheUtil cacheUtil;

    @Override
    public List<VideoBanner> listBanner() {
        try {
            Object res = cacheUtil.getTenMinutesCache().get(CacheKeyManager.BANNER_LIST_KEY, () -> {
                List<VideoBanner> bannerList = videoMapper.listBanners();
                return bannerList;
            });

            if (res instanceof List) {
                return (List<VideoBanner>) res;
            }

        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public VideoVO findVideoDetailByVideoId(Integer videoId) {
        return videoMapper.findVideoDetailByVideoId(videoId);
    }

    @Override
    public List<Video> findAllVideos() {
        try {
            Object res = cacheUtil.getTenMinutesCache().get(CacheKeyManager.VIDEO_LIST_KEY, () -> {
                List<Video> videoList = videoMapper.findAllVideos();
                return videoList;
            });

            if (res instanceof List) {
                return (List<Video>) res;
            }

        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
}
